package info.liujiachen.security.test.config;

import info.liujiachen.security.test.filter.JwtAuthenticationFilter;
import info.liujiachen.security.test.handler.JwtAuthenticationFailureHandler;
import info.liujiachen.security.test.handler.JwtAuthenticationSuccessHandler;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.logout.LogoutFilter;

public class JwtConfigurer<T extends JwtConfigurer<T, B>, B extends HttpSecurityBuilder<B>> extends AbstractHttpConfigurer<T, B> {

    private JwtAuthenticationFilter authFilter;

    public JwtConfigurer() {
        this.authFilter = new JwtAuthenticationFilter();
    }

    @Override
    public void configure(B http) throws Exception {

        authFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        authFilter.setAuthenticationSuccessHandler(new JwtAuthenticationSuccessHandler());
        authFilter.setAuthenticationFailureHandler(new JwtAuthenticationFailureHandler());

        JwtAuthenticationFilter filter = this.postProcess(authFilter);
        http.addFilterAfter(filter, LogoutFilter.class);

    }

}